package vip.idiy.anwei_android_iot.adapters;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.List;

import vip.idiy.anwei_android_iot.R;
import vip.idiy.anwei_android_iot.domin.DeviceInfo;


/**
 * ClassName RecyclerViewDeviceAdapter
 * describe: RecyclerView适配器 (设备)
 * Created: 勇
 * Email:118085890@qq.com
 * Version: 1.0
 * Create in 11:42 2022/8/18
 */
public class RecyclerViewDeviceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private String TAG = "myTag " + " RecyclerViewDeviceAdapter ";

    public static final int DEVICE_BASIC_TYPE = R.layout.layout_device_basic_list;
    public static final int ADD_TYPE = R.layout.layout_add_list;

    private Context context;
    private List<ViewType> viewTypes;


    /**
     * 添加数据
     *
     * @param types
     */
    public void addData(ViewType types) {
        viewTypes.add(types);
        // notifyDataSetChanged();
    }


    public RecyclerViewDeviceAdapter(Context context, List<ViewType> viewTypes) {
        this.context = context;
        this.viewTypes = viewTypes;
    }

    @Override
    public int getItemViewType(int position) {
        return viewTypes.get(position).getItemType();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DEVICE_BASIC_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_device_basic_list, parent, false);
            return new DeviceBasicHolder(view);
        }

        if (viewType == ADD_TYPE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_add_list, parent, false);
            return new LayoutAddHolder(view);
        }
        return null;
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case DEVICE_BASIC_TYPE:
                DeviceBasicHolder deviceBasicHolder = (DeviceBasicHolder) holder;
                // 转换
                DeviceInfo deviceBasicInfo = (DeviceInfo) viewTypes.get(position);
                deviceBasicHolder.device_basic_tv_deviceId.setText(deviceBasicInfo.getDeviceId());
                deviceBasicHolder.device_basic_tv_parentName.setText(deviceBasicInfo.getTypeName());
                deviceBasicHolder.device_basic_tv_sceneName.setText(deviceBasicInfo.getSceneName());
                deviceBasicHolder.device_basic_iv_rssi.setImageResource(deviceBasicInfo.getRssiDrawable());
                deviceBasicHolder.device_basic_tv_onLink.setText(deviceBasicInfo.isOnLine() ? "在线" : "离线");
                deviceBasicHolder.device_basic_iv_typeImg.setImageResource(deviceBasicInfo.getTypeImgDrawable());


                break;
            case ADD_TYPE:  // 新增
                LayoutAddHolder layoutAddHolder = (LayoutAddHolder) holder;
                AddView addView = (AddView) viewTypes.get(position);
                layoutAddHolder.rv_add_title.setText(addView.getTitle());
                // if (onItemClickListener != null) {
                //     layoutAddHolder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(v, position));
                // }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return viewTypes.size();
    }


    public class DeviceBasicHolder extends RecyclerView.ViewHolder {
        // 父级名称
        TextView device_basic_tv_parentName;
        // 场景名称
        TextView device_basic_tv_sceneName;
        // 是否在线
        TextView device_basic_tv_onLink;
        // 网络信号
        ImageView device_basic_iv_rssi;
        // 设备图片
        ImageView device_basic_iv_typeImg;
        // 设备ID
        TextView device_basic_tv_deviceId;

        public DeviceBasicHolder(@NonNull View v) {
            super(v);
            device_basic_tv_parentName = v.findViewById(R.id.device_basic_tv_parentName);
            device_basic_tv_sceneName = v.findViewById(R.id.device_basic_tv_sceneName);
            device_basic_tv_onLink = v.findViewById(R.id.device_basic_tv_onLink);
            device_basic_iv_rssi = v.findViewById(R.id.device_basic_iv_rssi);
            device_basic_iv_typeImg = v.findViewById(R.id.device_basic_iv_typeImg);
             device_basic_tv_deviceId = v.findViewById(R.id.device_basic_tv_deviceId);
        }
    }

    public class LayoutAddHolder extends RecyclerView.ViewHolder {
        TextView rv_add_title;

        public LayoutAddHolder(@NonNull View v) {
            super(v);
            rv_add_title = v.findViewById(R.id.rv_add_title);
        }
    }
}
